package com.controller;



import com.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @ClassName GenericController
 * @Reason
 * @Date 2020/8/1 14:35
 * @Author zhuyn
 **/
@Slf4j
public class GenericController {

    public static final String OK = "0";

    public static final String FAIL = "-1";

    protected HttpSession session;

    protected HttpServletRequest request;

    protected HttpServletResponse response;

    private InputStream ajaxResponse;


    /* 每次执行请求首先执行以下方法 */
    @ModelAttribute
    public void setServletApi(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setCharacterEncoding("utf-8");
    }

    public JSONObject getInputMessage() {

        StringBuffer buffer = new StringBuffer();
        try {
            InputStream is = request.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            String s = "";
            while ((s = br.readLine()) != null) {
                buffer.append(s);
            }
            String request = buffer.toString();
            log.info("********************接收到的报文为：" + request);
            if(request == null || "".equals(request)){
                return null;
            }
            JSONObject reJson = JSONUtils.toJSONObject(request);
            return reJson;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public String checkRequestMessage(JSONObject params) {

        //校验请求报文是否为空
        if(params == null || "".equals(params.toString())){
            return "请求报文为空";
        }

        //校验request_content是否为空
        JSONObject requestJson = params.getJSONObject("tradeMap");
        if(requestJson == null || "".equals(requestJson.toString())){
            return "请求报文为空";
        }

        return "";
    }

    /**
     * 构建校验不通过时，返回错误对象
     *
     * @param msg
     * @return
     */
    public JSONObject returnErrorObject(String msg) {
        JSONObject reObj = new JSONObject();
        reObj.put("code", FAIL);
        reObj.put("msg", msg);
        reObj.put("result_content", new JSONObject());
        return reObj;
    }

    /**
     *
     * 构建查询成功时，返回的结果对象
     *
     * @param data
     * @return
     */
    public JSONObject returnSuccessObject(JSONObject data) {
        JSONObject reObj = new JSONObject();
        reObj.put("code", OK);
        reObj.put("msg", "");
        reObj.put("result_content", data);
        return reObj;
    }



    /**
     *
     * 业务校验通过时，返回成功对象
     *
     * @param genericReturn
     * @return
     */
//    public JSONObject returnSuccOrFailObject(GenericReturn genericReturn) {
//        if (genericReturn.isHasError()) {
//            return returnErrorObject(genericReturn.getMsgContent());
//        } else {
//            return returnSuccessObject(genericReturn.getJsonObject());
//        }
//    }

    /**
     * 返回异常信息
     * @param e
     * @return
     */
    protected JSONObject returnErrorObject(Exception e) {
        JSONObject reObj = new JSONObject();
        reObj.put("code", FAIL);
        reObj.put("msg", e instanceof NullPointerException ? "发生空指针异常" : e.getMessage());
        reObj.put("result_content", new JSONObject());
        return reObj;
    }
}
